<?php
namespace backend\components;
use common\models\Admin\Admin;
use yii\helpers\Url;

/**
 * 独立于应用主题基类
 * @author Administrator
 *
 */
class BaseComponentController extends \common\components\BaseWebController
{
    const LAYER_CONTENT = "content";

    public $enableCsrfValidation = true;

    /**
     * 在这里面的就不用检查合法性
     * 登陆
     * @var array
     */
    protected $allowAllAction = [
        'login/index'
    ];

    /**
     * 权限合法性
     * @var array
     */
    protected $allowAuth = [

    ];//不检查权限数组

    protected $adminAllow = [
        "region-manager",
        "plug",
      //  "report/agent",
        "report/manager",
        "report/settlement",
        "percentage",
        "wx-public",
    ];

    public $userIsAdmin = false;
    public $userIsAgent = false;
    public $userIsRegionManager = false;
    public $uid;

    /**
     * 不检查权限数组
     * 权限合法性
     * @param string $key
     * @return array|bool|mixed
     */
    protected function allowAuth($key = "ALL")
    {
        $ret =  [

        ];

        if ( $key === "ALL" ) {
            return $ret;
        }
        return isset($ret[$key]) ? $ret[$key] : false;
    }

    public function __construct($id, $module, $config = [])
    {
        parent::__construct($id, $module, $config = []);
    }

    public function isAdmin()
    {
        return Admin::isAdminCurrent();
    }

    public function isManager()
    {
        return Admin::isRegionManagerCurrent();
    }

    public function isAgent()
    {
        return Admin::isAgentCurrent();
    }

    public function isAgentTop()
    {
        return Admin::isTopAgentCurrent();
    }

    public function setGuide($content, $color = "")
    {
        $contentStr = "";
        if ( is_array($content) ) {
            foreach ( $content as $key => $item ) {
                if ( is_array($color) ) {
                    $contentStr .= !empty($color[$key]) ? "<h4 style='color: ".$color[$key]."'>$item</h4>" : "<h4>$item</h4><br>";
                } else {
                    $contentStr .= $color ? "<h4 style='color: ".$color."'>$item</h4>" : "<h4>$item</h4><br>";
                }
            }
        } else {
            $contentStr = $color ? "<h4 style='color: ".$color."'>$content</h4>" : "<h4>$content</h4>";
        }
        \Yii::$app->view->params["guide"] = $contentStr;
    }

    public function beforeAction($action)
    {
        if ( !in_array($action->uniqueId, $this->allowAllAction) && !in_array($action->controller->uniqueId, $this->allowAllAction) ) {
            $user = new Admin();
            $loginStatus = $user->checkLogin(true);

            //未登录
            if ( !$loginStatus ) {
                if ( \Yii::$app->getRequest()->isAjax ) {
                    $this->jsonResponse(
                        self::RESPONSE_FAILURE,
                        '账户未登录！',[
                            self::RESPONSE_DATA_URL=>Url::to(['/login'])
                        ]
                    );
                    return false;
                }

                $this->redirect(Url::to(['/login']))->send();
                return false;
            }
            $userType = Admin::getCurrentUser("type");
            if ( $userType == Admin::TYPE_ADMIN ) {
                $this->userIsAdmin = true;
            } elseif ( $userType == Admin::TYPE_REGION_MANAGER ) {
                $this->userIsRegionManager = true;
            } elseif ( $userType == Admin::TYPE_AGENT ) {
                $this->userIsAgent = true;
            }
            $this->uid = Admin::getCurrentUser("id");


            //仅管理员可见
            if ( in_array($action->uniqueId, $this->adminAllow) || in_array($action->controller->uniqueId, $this->adminAllow) ) {
                if ( !Admin::isAdminCurrent() ) {
                    if ( \Yii::$app->getRequest()->isAjax ) {
                        $this->jsonResponse(
                            self::RESPONSE_FAILURE,
                            '无权访问！'
                        );
                        return false;
                    }

                    $this->redirect(Url::to(['/error']))->send();
                    return false;
                }
            }
        }

        return parent::beforeAction($action);
    }

    public function setTitle($title = "*"){
        $this->getView()->title = $title;
    }

}